using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Markup;
using Common;
using Common.Enum;
using Microsoft.Win32;
using SSettings;

namespace SettingsView.View
{
	public partial class UCBasicSetting : System.Windows.Controls.UserControl, IComponentConnector
	{
		public UCBasicSetting()
		{
			InitializeComponent();
			if (ConfigReader.GetInstance().GetProjectTypeFromIni(EProjectType.RQ) == 0)
			{
				allel_operation_chart.Visibility = Visibility.Collapsed;
			}
			int projectTypeFromIni = ConfigReader.GetInstance().GetProjectTypeFromIni(EProjectType.HRM);
			int projectTypeFromIni2 = ConfigReader.GetInstance().GetProjectTypeFromIni(EProjectType.Melt);
			if (projectTypeFromIni == 0 && projectTypeFromIni2 == 0)
			{
				melting_curve.Visibility = Visibility.Collapsed;
			}
			ExperimentPathPanel.Visibility = ((ConfigReader.GetInstance().GetFixExperimentPathFromIni() != 1) ? Visibility.Collapsed : Visibility.Visible);
			if (ConfigReader.GetInstance().GetSubsetConfigFromIni() == 0)
			{
				use_subset.Visibility = Visibility.Collapsed;
			}
			comboBoxExt.ItemsSource = new List<string> { "xlsx", "csv", "txt" };
		}

		private void btnSelectExportExe_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
			openFileDialog.Filter = string.Format("{0}{1}|*.{0}", "exe", Utility.GetMessage("File"));
			openFileDialog.FileName = string.Empty;
			openFileDialog.FilterIndex = 1;
			openFileDialog.RestoreDirectory = true;
			openFileDialog.DefaultExt = "exe";
			if (openFileDialog.ShowDialog() == true)
			{
				ConfigSettings configSettings = base.DataContext as ConfigSettings;
				if (configSettings != null)
				{
					configSettings.ExperimentSetting.CustomExportName = openFileDialog.FileName;
				}
				if (openFileDialog.FileName != ExperimentSetting.InternalCustomExportName)
				{
					File.Copy(openFileDialog.FileName, ExperimentSetting.InternalCustomExportName, true);
				}
			}
		}

		private void btnSelectImportExe_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
			openFileDialog.Filter = string.Format("{0}{1}|*.{0}", "exe", Utility.GetMessage("File"));
			openFileDialog.FileName = string.Empty;
			openFileDialog.FilterIndex = 1;
			openFileDialog.RestoreDirectory = true;
			openFileDialog.DefaultExt = "exe";
			if (openFileDialog.ShowDialog() != true)
			{
				return;
			}
			ConfigSettings configSettings = base.DataContext as ConfigSettings;
			if (configSettings != null)
			{
				configSettings.BasicSetting.CustomImportName = Path.GetFileName(openFileDialog.FileName);
				if (openFileDialog.FileName != configSettings.BasicSetting.CustomImportFullName)
				{
					File.Copy(openFileDialog.FileName, configSettings.BasicSetting.CustomImportFullName, true);
				}
			}
		}

		private void btnDefaultColorSetting_Click(object sender, RoutedEventArgs e)
		{
			ConfigSettings configSettings = base.DataContext as ConfigSettings;
			if (configSettings != null)
			{
				DefaultColorWindow defaultColorWindow = new DefaultColorWindow(configSettings.ExperimentSetting.ColorSettings);
				defaultColorWindow.Owner = Utility.GetMainWindow();
				defaultColorWindow.ShowDialog();
			}
		}

		private void btnBrowse_Click(object sender, RoutedEventArgs e)
		{
			FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
			folderBrowserDialog.SelectedPath = textPath.Text;
			folderBrowserDialog.ShowNewFolderButton = true;
			if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
			{
				textPath.Text = folderBrowserDialog.SelectedPath;
			}
		}
	}
}
